package com.css.fxfzfxqh.modules.comprehensiveSearch.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.comprehensiveSearch.param.FuzzyQueryParam;
import com.css.fxfzfxqh.modules.comprehensiveSearch.param.QueryParam;
import com.css.fxfzfxqh.modules.comprehensiveSearch.service.CalculationResultService;
import com.css.fxfzfxqh.modules.taskMaps.vo.MapsVO;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/06/20
 * @Description: 查询和导出计算结果controller
 */
@Slf4j
@RestController
@RequestMapping("/calculationResult")
public class CalculationResultController {

    @Resource
    private CalculationResultService calculationResultService;

    /**
     * 按评估/区划条件查询
     *
     * @param param 参数
     * @return page
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request, QueryParam param) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = calculationResultService.getPage(curPage, pageSize, param);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 按条件模糊查询
     *
     * @param param 条件
     * @return page
     */
    @GetMapping("/conditionalFuzzyQuery")
    public RestResponse conditionalFuzzyQuery(HttpServletRequest request, FuzzyQueryParam param) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = calculationResultService.conditionalFuzzyQuery(curPage, pageSize, param);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 按评估/区划条件查看评估图件
     *
     * @param param 参数
     * @return page
     */
    @GetMapping("/getMaps")
    public RestResponse getMaps(QueryParam param) {
        RestResponse restResponse = null;
        try {
            List<MapsVO> maps = calculationResultService.getMaps(param);
            restResponse = RestResponse.succeed(maps);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 按评估/区划条件下载评估图件
     *
     * @param param 参数
     * @return page
     */
    @PostMapping("/downloadMaps")
    public void downloadMaps(HttpServletResponse response, @RequestBody QueryParam param) {
        calculationResultService.downloadMaps(response, param);
    }

    /**
     * 按条件模糊查询下载评估图件
     *
     * @param param 参数
     * @return page
     */
    @PostMapping("/downloadMaps1")
    public void downloadMaps1(HttpServletResponse response, @RequestBody Map<String,String> param) {
        calculationResultService.downloadMaps1(response, param);
    }
}
